package com.canbot.u05.activity.presidemode.newcode.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IHeadCommonClick;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HeadCommonView extends RelativeLayout {
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.centerTitle)
    TextView centerTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    private IHeadCommonClick iHeadCommonClick;


    @OnClick({R.id.tv_left, R.id.tv_right})
    public void viewClick(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                if (iHeadCommonClick != null) {
                    iHeadCommonClick.onClick(IHeadCommonClick.LEFT);
                }
                break;
            case R.id.tv_right:
                if (iHeadCommonClick != null) {
                    iHeadCommonClick.onClick(IHeadCommonClick.RIGHT);
                }
                break;
        }
    }

    public void setiHeadCommonClick(IHeadCommonClick iHeadCommonClick) {
        this.iHeadCommonClick = iHeadCommonClick;
    }

    public HeadCommonView(Context context) {
        this(context, null);
    }

    public HeadCommonView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public HeadCommonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initStyle(context, attrs);
    }

    public void setCenterTitle(String centerTitle) {
        this.centerTitle.setText(centerTitle);
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.host_head_common_view, this);
        ButterKnife.bind(this, view);
    }

    private void initStyle(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HeadCommonView);
        int leftTextId = typedArray.getResourceId(R.styleable.HeadCommonView_leftText, -1);
        int rightTextId = typedArray.getResourceId(R.styleable.HeadCommonView_rightText, -1);
        int centerTextId = typedArray.getResourceId(R.styleable.HeadCommonView_centerText, -1);
        Drawable leftDrawableRes = typedArray.getDrawable(R.styleable.HeadCommonView_leftDrawable);
        Drawable rightDrawableRes = typedArray.getDrawable(R.styleable.HeadCommonView_rightDrawable);
        if (leftDrawableRes != null && leftTextId != -1) {
            tvLeft.setVisibility(VISIBLE);
            tvLeft.setText(getContext().getString(leftTextId));
            leftDrawableRes.setBounds(0, 0, leftDrawableRes.getMinimumWidth(), leftDrawableRes.getMinimumHeight());
            tvLeft.setCompoundDrawables(leftDrawableRes, null, null, null);
        }

        if (rightDrawableRes != null && rightTextId != -1) {
            tvRight.setVisibility(VISIBLE);
            tvRight.setText(getContext().getString(rightTextId));
            rightDrawableRes.setBounds(0, 0, leftDrawableRes.getMinimumWidth(), leftDrawableRes.getMinimumHeight());
            tvRight.setCompoundDrawables(rightDrawableRes, null, null, null);
        }

        if (centerTextId != -1) {
            centerTitle.setVisibility(VISIBLE);
            centerTitle.setText(context.getString(centerTextId));
        }
        typedArray.recycle();
    }
}
